package LogicSide;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import DBside.JDBCConnection;
import DBside.Tools;


public class client extends AbstarctClient{
	
	private static String[] getAttruibutes(ResultSetMetaData resultSetMetaData) {
		try {
			String[] attributes = new String[resultSetMetaData.getColumnCount()];
			
			for(int i = 1; i <= resultSetMetaData.getColumnCount(); i++) {
				attributes[i - 1] = resultSetMetaData.getColumnName(i);
			}   
			return attributes;
		} catch (SQLException e) {
			Tools.printError("getAttruibutes", e);
			return null;
		}
	}
	
	public static Table selectPlayList(ResultSet resultSet, Composite composite){
		ResultSetMetaData resultSetMetaData = null;
		
		try {
			resultSetMetaData = resultSet.getMetaData();
		} catch (SQLException e) {
			Tools.printError("playList", e);
		}
		
		Table table = new Table (composite, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible (true);
		table.setHeaderVisible (true);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 200;
		table.setLayoutData(data);
		
		String[] titles = getAttruibutes(resultSetMetaData);

		for (int i=0; i<titles.length; i++) {
			TableColumn column = new TableColumn (table, SWT.NONE);
			column.setText (titles [i]);
		}	
		
		//add rows to table from resultSet
		try {
			while(resultSet.next()) {
				TableItem item = new TableItem (table, SWT.NONE);
				for(int i = 0; i < titles.length; i++){
					item.setText (i, resultSet.getObject(i + 1).toString());
				}			
			}
		} catch (SQLException e) {
			Tools.printError("playList", e);
		}
		
		
		for (int i=0; i<titles.length; i++) {
			table.getColumn (i).pack ();
		}
		return table;
	}
	
	public static void main(String[] args){
	/*	JDBCConnection session = new JDBCConnection();
		session.openConnection(Tools.getHomeURL(), Tools.getUserName(), Tools.getPassword());
		
		String attributes = "*";
		String[] conditions = new String[0];
		
//		ResultSet resultSet = session.select(attributes ,conditions, DBside.JDBCConnection.Table.SONG);
		
		Display display = new Display ();
		Shell shell = new Shell (display);
		shell.setLayout(new GridLayout());
		
		selectPlayList(resultSet, shell);
		
		//shell.pack ();
		shell.open ();
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ()) display.sleep ();
		}
		display.dispose ();
		
		session.closeConnection();*/
	}
	
	@Override
	public int addToPlayList(int songID, int playListID) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int removeFromPlayList(int songID, int playListID) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int search(String input) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int openPlayList(int playListID) {
		// TODO Auto-generated method stub
		return 0;
	}

}
